﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using GalleryCommon;
using System.Collections;
using System.Collections.Generic;

public partial class MainView : System.Web.UI.Page
{
    int LastPicNo;
    bool IsLastPage;

    // protected void Page_Load(object sender, EventArgs e)
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["User"] == null)
        {
            Response.Redirect("login.aspx");
        }
        if (ViewState["LastPicNo"]==null)
        {
            ViewState["LastPicNo"] = 0;
        }
        else
        {
            LastPicNo = (int)ViewState["LastPicNo"];
        }

        User user = (User)Session["User"];
        GenerateMiddleTable(user);
    }
    
    //dinamikusan létrehozzuk a táblázatban a táblázatot
    protected void GenerateMiddleTable(User user)
    {
        int count = 0;
        List<Picture> pictures;
        GalleryLogic.PictureLogic.ProvidePicture(user, out pictures, LastPicNo);
        TableMiddle.Rows.Clear();
        for (int i = 0; i < 3; i++)
        {
            HtmlTableRow row = new HtmlTableRow();
            for (int j = 0; j < 3; j++)
                {
                HtmlTableCell cell = new HtmlTableCell();
                HtmlImage img = new HtmlImage();
                try
                {
                    if (pictures.Count > count)
                    {
                        img.Src = "data:image/png;base64," + Convert.ToBase64String(pictures[count].PictureFile);
                        img.Style.Add("max-width", "30%");
                        img.Style.Add("max-height", "30%");
                        img.Attributes["class"] = "thumbnail";
                        img.Attributes["onClick"] = string.Format("location.href='Image.aspx?PicID={0}';", pictures[count].PictureID);
                        cell.Controls.Add(img);
                        count++;
                    }
                   
                }
                catch { }
                row.Cells.Add(cell);
            }
            TableMiddle.Rows.Add(row);
        }
        if (count<9) { IsLastPage = true; } else { IsLastPage = false; }
    }
 
  
    //image kiszedés fileupload controlból és konvertálás byte-ba 
    protected void ButtonUpload_Click(object sender, EventArgs e)
    {
        User user = (User)Session["User"];
        long AlbumID = user.ActiveAlbumID;
        bool fileOK = false;
        if (FileUpload.HasFile)
        {
            string fileExtension =
                System.IO.Path.GetExtension(FileUpload.FileName).ToLower();
            string[] allowedExtensions =
                {".gif", ".png", ".jpeg", ".jpg"};
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
        }

        if (fileOK)
        {
            try
            {
                byte[] fileData = null;
                using (var binaryReader = new System.IO.BinaryReader(Request.Files[0].InputStream))
                {
                    fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
                }
                GalleryLogic.PictureLogic.AddNewPicture(FileUpload.FileName, AlbumID, fileData);
 
            }
            catch (Exception ex)
            {
                Response.Write("File could not be uploaded." + ex.ToString());
            }
        }
        else
        {
            Response.Write("Cannot accept files of this type.");
        }
        ClientScript.RegisterStartupScript(typeof(Page), "autoPostback", ClientScript.GetPostBackEventReference(this, String.Empty), true);
    }

    protected void ButtonNext_Click(object sender, EventArgs e)
    {
        if (!IsLastPage)
        {
            LastPicNo = LastPicNo + 9;
        }
        ClientScript.RegisterStartupScript(typeof(Page), "autoPostback", ClientScript.GetPostBackEventReference(this, String.Empty), true);
    }

    protected void ButtonPrevious_Click(object sender, EventArgs e)
    {
        if (LastPicNo > 0)
        {
        LastPicNo=LastPicNo-9;
        }
        ClientScript.RegisterStartupScript(typeof(Page), "autoPostback", ClientScript.GetPostBackEventReference(this, String.Empty), true);

    }

     protected void ButtonAlbumView_Click(object sender, EventArgs e)
    {
        Response.Redirect("AlbumView.aspx");
    }

    void Page_PreRender(object sender, EventArgs e)
    {
        ViewState["LastPicNo"] = LastPicNo;
    }
}